import 'package:flutter/material.dart';
import 'tabbar_page.dart';
import 'animation_test.dart';
import 'dialog_test.dart';

///1.演示Tabbar使用
///
///2.pageView的使用
///
///3.bottombar的使用
///
class AppHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return AppHomeState();
  }
}

class AppHomeState extends State<AppHomePage>
    with SingleTickerProviderStateMixin {
  int _index = 0;
  PageController _pageViewController;

  @override
  void initState() {
    super.initState();
    _pageViewController = PageController(initialPage: 0);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          '测试',
          style: TextStyle(fontSize: 16),
        ),
      ),
      body: _buildHomeView(),
//      bottomNavigationBar: _buildNavigateBottomBar(),
      bottomNavigationBar: _buildBottomAppBar(),
//      floatingActionButton: FloatingActionButton(
//        onPressed: () {
//          switchPage(2);
//        },
//        backgroundColor: Color(0xFFBA68C8),
//        child: Icon(
//          Icons.add,
//          color: Colors.white,
//        ),
//      ),
//      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }

  Widget _buildHomeView() {
    return PageView(
//      physics: NeverScrollableScrollPhysics(),
      onPageChanged: (page) {
        setState(() => _index = page);
      },
      controller: _pageViewController,
      children: <Widget>[
        Container(
          child: Center(
            child: Text('I am Home'),
          ),
        ),
//        _buildListView(),
//        Container(
//          child: Center(
//            child: Text('I am favorate'),
//          ),
//        )
        TabBarPage(),
//        AnimationTest(),
        DialogTest(),
      ],
    );
  }

  Widget _buildBottomAppBar() {
    return BottomAppBar(
        color: Color(0xFFC5E1A5),
        child: Container(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              GestureDetector(
                onTap: () {
                  switchPage(0);
                },
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Icon(
                      Icons.home,
                      color: _index == 0 ? Color(0xFFEF9A9A) : Colors.grey,
                      size: 24,
                    ),
                    Text(
                      "首页",
                      style: TextStyle(
                          color: _index == 0 ? Color(0xFFEF9A9A) : Colors.grey),
                    )
                  ],
                ),
              ),
              GestureDetector(
                onTap: () {
                  switchPage(1);
                },
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Icon(
                      Icons.favorite,
                      color: _index == 1 ? Color(0xFFEF9A9A) : Colors.grey,
                      size: 24,
                    ),
                    Text(
                      "喜好",
                      style: TextStyle(
                          color: _index == 1 ? Color(0xFFEF9A9A) : Colors.grey),
                    )
                  ],
                ),
              ),
              GestureDetector(
                onTap: () {
                  switchPage(2);
                },
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Icon(
                      Icons.add_a_photo,
                      color: _index == 2 ? Color(0xFFEF9A9A) : Colors.grey,
                      size: 24,
                    ),
                    Text(
                      "拍照",
                      style: TextStyle(
                          color: _index == 2 ? Color(0xFFEF9A9A) : Colors.grey),
                    )
                  ],
                ),
              ),
//              IconButton(
//                icon: Icon(
//                  Icons.home,
//                  color: _index == 0 ? Color(0xFFEF9A9A) : Colors.white,
//                ),
//                onPressed: () {
//                  switchPage(0);
//                },
//              ),
//              IconButton(
//                icon: Icon(
//                  Icons.favorite,
//                  color: _index == 1 ? Color(0xFFEF9A9A) : Colors.white,
//                ),
//                onPressed: () {
//                  switchPage(1);
//                },
//              ),
            ],
          ),
          height: 48,
        )

//      shape: CircularNotchedRectangle(),
        );
  }

  void switchPage(int page) {
    setState(() {
      _index = page;
    });
    _pageViewController.jumpToPage(_index);
  }

  Widget _buildNavigateBottomBar() {
    return BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      fixedColor: Colors.deepPurple,
      items: [
        BottomNavigationBarItem(
          icon: new Icon(Icons.home),
          title: new Text("home"),
        ),
        BottomNavigationBarItem(
          icon: new Icon(Icons.favorite),
          title: new Text("fravorite"),
        ),
      ],
      currentIndex: _index,
      onTap: switchPage,
    );
  }

  Widget _buildListView() {
    return ListView.separated(
        itemBuilder: (context, itemIndex) {
          return Padding(
              padding: EdgeInsets.symmetric(vertical: 8),
              child: Text('$itemIndex'));
        },
        separatorBuilder: (context, index) {
          return Divider(
            height: 1,
          );
        },
        itemCount: 60);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }
}
